<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>建造者模式(Bulider pattern)</title>
  </head>
  <body>
    <script>
      class NavBar {
        init() {
          console.log("NavBar-init");
        }

        getData() {
          return new Promise((resolve) => {
            console.log("NavBar-getData");
            setTimeout(() => {
              console.log("NavBar data loading success");
              resolve({ data: "111" });
            }, 1000);
          });
        }

        render() {
          return new Promise((resolve) => {
            console.log("NavBar-render");
            setTimeout(() => {
              console.log("NavBar render success");
              resolve({ html: "<div>111</div>" });
            }, 200);
          });
        }
      }

      class List {
        init() {
            console.log("List-init");
        }

        getData() {
            console.log("List-getData");
        }

        render() {
            console.log("List-render");
        }
      }

      class Operator {
        async startBuild(builder) {
          // 初始化
          await builder.init();
          // 获取数据
          const dataRes = await builder.getData();
          if (dataRes) {
            console.log("dataRes:", dataRes);
          }
          // 渲染组件
          const renderRes = await builder.render();
          if (renderRes) {
            console.log("renderRes:", renderRes);
          }
        }
      }

      const op = new Operator();
      const navBar = new NavBar();
      const list = new List()
      op.startBuild(navBar);
      op.startBuild(list);
    </script>
  </body>
</html>
